package com.liuchuanzheng.lcz_wanandroid.module.mine.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone
import com.blankj.utilcode.util.AppUtils
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.base.BaseVMActivity
import com.liuchuanzheng.lcz_wanandroid.databinding.ActivitySettingsBinding
import com.liuchuanzheng.lcz_wanandroid.ext.showToast
import com.liuchuanzheng.lcz_wanandroid.module.empty.activity.EmptyActivity
import com.liuchuanzheng.lcz_wanandroid.module.mine.viewmodel.SettingViewModel
import com.liuchuanzheng.lcz_wanandroid.module.splash.activity.SplashActivity
import com.liuchuanzheng.lcz_wanandroid.module.user.UserManager
import com.liuchuanzheng.lcz_wanandroid.util.isNightMode
import com.liuchuanzheng.lcz_wanandroid.util.setNightMode

class SettingsActivity : BaseVMActivity<SettingViewModel>() {

    lateinit var mViewBinding: ActivitySettingsBinding
    companion object {
        fun start(context: Context) {
            Intent(context, SettingsActivity::class.java).run {
                context.startActivity(this)
            }
        }
    }
    /**
     * 因为系统自带的设置view方法有很多种,id,view都可以.所以这里不像
     * 大家都用id的封装方式,那是限制了自己
     * 建议用viewBinding的方式。个人不喜欢dataBinding
     */
    override fun setContentView() {
        mViewBinding = ActivitySettingsBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
    }
    /**
     * 进行观察订阅
     */
    override fun observeViewModel() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding.ivBack.setOnClickListener { finish() }
        mViewBinding.scDayNight.isChecked = isNightMode(this)
        val appVersionName = AppUtils.getAppVersionName()
        val appVersionCode = AppUtils.getAppVersionCode()
        mViewBinding.tvCheckVersion.text = "当前版本：V${appVersionName}"

        mViewBinding.scDayNight.setOnCheckedChangeListener { _, checked ->
            setNightMode(checked)
        }
        mViewBinding.llFontSize.setOnClickListener {
            showToast(R.string.coming_soon)
        }
        mViewBinding.tvLogout.isGone = !UserManager.isLogin()
        mViewBinding.tvLogout.setOnClickListener {
            AlertDialog.Builder(this)
                .setMessage(R.string.confirm_logout)
                .setPositiveButton(R.string.confirm) { _, _ ->
                    mViewModel.logout()
                    val intent = Intent(activity, SplashActivity::class.java)
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(intent)

                }
                .setNegativeButton(R.string.cancel) { _, _ -> }
                .show()
        }

    }
}
